package com.rpframework.website.friendly.web.directive;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.rpframework.core.freemarker.directive.BaseTemplateDirectiveModel;
import com.rpframework.core.freemarker.directive.DirectiveUtils;
import com.rpframework.core.utils.DictionarySettingUtils;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component("friendly")
public class YLDirectiveModel extends BaseTemplateDirectiveModel {

	final Logger logger = LoggerFactory.getLogger(getClass());
	static final String GET_VILLAGE_LIST = "get_village_list";
	static final String GET_ROOM_LIST = "get_room_list";
	static final String GET_HOBBIES_LIST = "get_hobbies_list";

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		String cmd = DirectiveUtils.getString("cmd", params);
		Map paramWarp = new HashMap(params);
		boolean pass = true;
		if (StringUtils.isBlank(cmd)) {

		} 
		else if (StringUtils.equals(cmd, GET_HOBBIES_LIST)) {
			Map map = DictionarySettingUtils.getParameterMap("hobbies.type");//获取数据字典里 该类型的所有值
			paramWarp.put("hobbiesList", ObjectWrapper.DEFAULT_WRAPPER.wrap(map));
		}
		Map origWarp = DirectiveUtils.addParamsToVariable(env, paramWarp);
		if (pass && body != null) {
			body.render(env.getOut());
		}
		DirectiveUtils.removeParamsFromVariable(env, paramWarp, origWarp);
	}
}
